package {{.tf.Package}};
{{- $tfModel := ""}}
{{- $tfService := ""}}
{{- $tfMapper := ""}}
{{- $tfDto := ""}}
{{- range .tplFiles}}
{{- if eq .Type "Model"}}{{$tfModel = .}}{{- end}}
{{- if eq .Type "Service"}}{{$tfService = .}}{{- end}}
{{- if eq .Type "Mapper"}}{{$tfMapper = .}}{{- end}}
{{- if eq .Type "Dto"}}{{$tfDto = .}}{{- end}}
{{- end}}

{{- $model := toCamel .table.Name}}{{- /*Mode名字*/}}
{{- $mapper := printf "%s%s" $model "Mapper" }}{{- /*Mapper*/}}
{{- $service := printf "%s%s%s" "I" $model "Service" }}{{- /*接口*/}}
{{- .ctx.Set "fileName" (printf "%s%s" $model "ServiceImpl")}}{{- /* 设置输出文件名字 */}}

import {{$tfModel.Package}}.{{$model}};
import {{$tfMapper.Package}}.{{$mapper}};
import {{$tfService.Package}}.{{$service}};
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travel.customized.dto.ResultDTO;
import com.travel.customized.entity.PageList;
import com.travel.customized.entity.Result;
import com.travel.customized.exception.BizException;
import com.travel.customized.utils.BeanUtils;
import com.travel.customized.utils.ModelUtils;
import com.travel.customized.utils.SnowflakeUtils;
import {{$tfDto.Package}}.*;

import java.util.List;

/**
 * {{.table.Comment}} 服务实现类
 *
 * @author {{.tpl.Author}}
 * @since {{.datetime}}
 */
@Service
public class {{$model}}ServiceImpl extends ServiceImpl<{{$mapper}}, {{$model}}> implements {{$service}} {

	@Override
    public ResultDTO<List<{{$model}}ListRspDTO>> list({{$model}}ListReqDTO dto) {
        {{$model}} query = BeanUtils.copyProperties(dto, {{$model}}.class);
		query.setDelFlag(0);
        QueryWrapper<{{$model}}> queryWrapper = new QueryWrapper<>(query);
        List<{{$model}}> list = this.list(queryWrapper);
        List<{{$model}}ListRspDTO> retList = BeanUtils.copyList(list, {{$model}}ListRspDTO.class);
        return ResultDTO.success(retList);
    }

    @Override
    public ResultDTO<PageList<{{$model}}ListRspDTO>> listPage(Integer page, Integer pageSize, {{$model}}ListReqDTO dto) {

		if (page == null || pageSize == null || page < 0 || pageSize < 0) {
		    throw BizException.create("分页参数不合法", null);
		}

		{{$model}} query = BeanUtils.copyProperties(dto, {{$model}}.class);
		query.setDelFlag(0);
		Page<{{$model}}> p = new Page<>(page, pageSize);
		p = this.page(p, new QueryWrapper<>(query));
		List<{{$model}}> list = p.getRecords();

		PageList<{{$model}}ListRspDTO> pageList = new PageList<>(p.getCurrent(), p.getSize(), p.getPages(), p.getTotal());
		pageList.setRows(BeanUtils.copyList(list, {{$model}}ListRspDTO.class));
		pageList.setTotal(p.getTotal());

		return ResultDTO.success(pageList);
    }

    @Override
    public ResultDTO<{{$model}}AddRspDTO> add({{$model}}AddReqDTO dto) {

		Result<?> r = checkForAdd(dto);
		if (r.isFailed()) {
		    return ResultDTO.failed(r.getMsg(), null);
		}

		{{$model}} add = BeanUtils.copyProperties(dto, {{$model}}.class);
		add.setId(SnowflakeUtils.nextId());
		ModelUtils.updateForAdd(add, null);
		boolean succ = this.save(add);
		if (!succ) {
		    return ResultDTO.failed("添加失败", null);
		}

		return ResultDTO.success();
    }

    private Result<?> checkForAdd({{$model}}AddReqDTO dto) {
		return Result.success();
    }

    @Override
    public ResultDTO<{{$model}}UpdRspDTO> update({{$model}}UpdReqDTO dto) {

		Result<?> r = checkForUpd(dto);
		if (r.isFailed()) {
		    return ResultDTO.failed(r.getMsg(), null);
		}

		{{$model}} update = BeanUtils.copyProperties(dto, {{$model}}.class);
		ModelUtils.updateForUpdate(update, null);
		boolean succ = this.updateById(update);
		if (!succ) {
		    return ResultDTO.failed("修改失败", null);
		}

		return ResultDTO.success();

    }

    private Result<?> checkForUpd({{$model}}UpdReqDTO dto) {
		return Result.success();
    }

    @Override
    public ResultDTO<{{$model}}DelRspDTO> delete({{$model}}DelReqDTO dto) {

		Result<?> r = checkForDel(dto);
		if (r.isFailed()) {
		    return ResultDTO.failed(r.getMsg(), null);
		}

		{{$model}} update = BeanUtils.copyProperties(dto, {{$model}}.class);
		ModelUtils.updateForUpdate(update, null);
		update.setDelFlag(1);
		boolean succ = this.updateById(update);
		if (!succ) {
		    return ResultDTO.failed("删除失败", null);
		}

		return ResultDTO.success();

    }

    private Result<?> checkForDel({{$model}}DelReqDTO dto) {
		return Result.success();
    }
}
